package com.filldream.fastboot.core;


import com.filldream.fastboot.base.entity.Role;
import com.filldream.fastboot.base.response.app.AccountManagerDTO;
import com.filldream.fastboot.base.response.pc.SystemLoginDTO;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.util.Assert;

public class LoginHolder {

    private static ThreadLocal<AccountManagerDTO> userInfoThreadLocal = new ThreadLocal<AccountManagerDTO>();

    public static AccountManagerDTO getUserInfo() {
        AccountManagerDTO accountManagerDTO = userInfoThreadLocal.get();
        Assert.isNull(accountManagerDTO, ErrorCode.NO_LOGIN.reMsg());
        return accountManagerDTO;
    }
    public static Long getUserId(){
        AccountManagerDTO userInfo = getUserInfo();
        return userInfo.getId();
    }
    public static AccountManagerDTO getUserInfoOrNull() {
        return userInfoThreadLocal.get();
    }
    public static Long getUserIdOrNull(){
        AccountManagerDTO accountManagerDTO = userInfoThreadLocal.get();
        if( accountManagerDTO == null ){
            return null;
        }
        return accountManagerDTO.getId();
    }

    public static void setUserInfo(AccountManagerDTO userInfo) {
        userInfoThreadLocal.set(userInfo);
    }

    //后端ThreadLocal
    private static ThreadLocal<SystemLoginDTO> pcUserThreadLocal = new ThreadLocal<SystemLoginDTO>();

    public static SystemLoginDTO getPcUser() {
        SystemLoginDTO systemLoginDTO = pcUserThreadLocal.get();
        Assert.isNull(systemLoginDTO, ErrorCode.NO_LOGIN.reMsg());
        return systemLoginDTO;
    }

    public static SystemLoginDTO getPcUserOrNull() {
        SystemLoginDTO systemLoginDTO = pcUserThreadLocal.get();
        return systemLoginDTO;
    }

    public static void remove(){
        pcUserThreadLocal.remove();
        userInfoThreadLocal.remove();
    }


    public static Long getPcUserId() {
        return getPcUser().getId();
    }

    public static Long getPcRoleId() {
        return getPcUser().getRoleId();
    }

    public static Role getPcUserRole(){
        return getPcUser().getRole();
    }

    public static void setPcUser(SystemLoginDTO userInfo) {
        pcUserThreadLocal.set(userInfo);
    }

}
